﻿@model yq.IBase.Table.TableInfo
@{
    Layout = null;
}
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text;
using System.Data; 
using yq.IBase;
using yq.Model.Logic;
using System.Collections; 
using System.Data.Common;
using yq.IBase.Basis;
using Newtonsoft.Json;

namespace yq.Model.Base 
{
    ///<summary>
    /// @(Model.Name) - 对象基础类
    /// </summary>
    ///<typeparam name="T"></typeparam>
    public abstract class I@(Model.ClassName)<T> :ModelBase<@(Model.ClassName)> where T : @(Model.ClassName), new()
    {
        /// <summary>
        /// 字段列表
        /// </summary>
        protected override List<string> GetColumns()
        {
            return new List<string>() { "@Html.Raw( string.Join("\",\"", Model.Columns.Select(obj=>obj.Code).ToArray()))" };
        }

        /// <summary>
        /// 表名
        /// </summary>
        protected override string GetTableName()
        {
            return "@Model.Code";
        }


        /// <summary>
        /// 写入主键值
        /// </summary>
        /// <param name="Id">主键</param>
        public override void SetKeyValue(dynamic Id)
        {
            this.@(Model.Columns[0].Code) = Id;
        }

        /// <summary>
        /// 获取主键值
        /// </summary>
        /// <returns></returns>
        public override dynamic GetKeyValue()
        {
            return @(Model.Columns[0].Code);
        }

        /// <summary>
        /// 为本对象赋值
        /// </summary>
        /// <param name="dy"></param>
        public override void SetValue(dynamic dy) 
        {
             @foreach (var info in Model.Columns)
            {
           @("           this.")@(info.Code)@("=")@("dy.")@(info.Code+";");
                         @("\r\n")                               
             }
        } 

        #region 基本属性定义

@foreach (var info in Model.Columns)
{
    if (info.Code.Equals("Version"))
    {
        continue;
    }
    
    
     <text>
         /// <summary>
        /// @info.Name
        /// </summary>
        private @(Tool.GetTypeName(info.DataType)) _@(info.Code) @Html.Raw(Tool.GetTypeValue(info.DataType,info.Stereotype));
      
        /// <summary>
        /// @info.Name
        /// </summary>
        public @(Tool.GetTypeName(info.DataType,info.Stereotype,Model.ClassName,info.Code)) @(info.Code)
        {
            get 
            {
              return@(Tool.StringGet(info.Stereotype, Model.ClassName, info.Code))
            }

            set 
            {
            @(Tool.StringSet(info.Stereotype, Model.ClassName, info.Code))
            }
        }
       </text>
}

      #endregion 基本属性定义 

      #region 扩展属性定义

@foreach (var info in Model.Columns.Where(obj => obj.Stereotype != "enum" && obj.Stereotype != "bool"&& obj.Stereotype!=""))
{
    if (info.Code.Equals("Version"))
    {
        continue;
    }

    string tablename=info.Stereotype.Replace("_", "");
   
    
     <text>  @(tablename) _@(tablename);

        /// <summary>
        /// @(tablename) 外键关联 @(info.Code)（@info.Name）   
        /// </summary>
        [ForeignKey("@(info.Code)")]
        [JsonIgnore]
        public @(tablename) @(tablename)
        {
            get
            {
                if (_@(tablename)==null&&@(info.Code)!=null)
                {
                    _@(tablename) = new @(tablename)().Load(@(info.Code));
                } 
                return _@(tablename);
            }
            protected set { _@(tablename) = value; }
        }</text>
    
     
}

      #endregion 扩展属性定义 

          protected  override void SetValue(@(Model.ClassName) info, DataRow row) 
          { 
               if (row == null) 
               { 
                 info.IsLoad = false; 
               }
               else 
               { 
                  info.IsLoad = true; 
            @foreach (var info in Model.Columns)
            {
                  <text>if(row["@info.Code"]!= DBNull.Value)
                  {
                     info.@(info.Code)=@Html.Raw(Tool.GetConvert(info.DataType, info.Code, info.Stereotype, Model.ClassName));
                  }</text>
            }
               } 

          } 

        protected  override void GetDbParameters(DbCommand comm, @(Model.ClassName) info, DbHelper cn) 
        { 
         @foreach (var info in Model.Columns)
         {
             if (!info.Code.Equals("Version"))
             {
             <text>cn.AddInParameter(comm, "@@@(info.Code)", DbType.@Tool.GetDbTypeName(info.DataType),ParameterTool.GetObj( info._@(info.Code) ));</text>
             }
             else
             {
             <text>cn.AddInParameter(comm, "@@Version", DbType.String, Guid.NewGuid().ToString());
             cn.AddInParameter(comm, "@@_versionOdl", DbType.String, info._Version); </text>
             }
         }
        } 

    } 
} 